GBML manual / Tile Animation

Tile animation

A tileset can mark some tiles as animated, so a patch of water or a torch flickers on its own, everywhere it is painted, for almost no cost.

What an animated tile is

A room's floor and walls are painted out of tiles: small square pictures, 8 pixels wide and 8 tall, that the editor lets you stamp onto a grid like a rubber stamp. All the tiles you can paint with come from one picture, called a tileset. Each tile in it has a number, and a painted cell on the room's grid just remembers that number.

An animated tile is a tile whose picture changes over time. You tell the tileset "tile 1 should cycle through the pictures of tiles 1, 2, 3 and 4, and move on every 12 frames." After that the water ripples, or the torch flickers, with no code at all.

Here is the trick that makes it cheap. The room's grid never changes. Every cell still says "tile 1". What changes is what tile 1 looks like. The game keeps a copy of the tile pictures in the screen's own memory, and tile animation copies the next picture over tile 1's slot. Every cell that says "tile 1" shows the new picture the instant the copy happens, because they all look at the same slot.

That is why animation belongs on a tileset and not on a room. A tileset that animates its water animates it in every room that uses the tileset, and you only set it up once.

What it costs

Tile animation is one of the cheapest things a Game Boy Advance game can do, and here is why.

This page has no measured number for it, because none has been taken from a real game. You can measure your own. The frame profiler gives tile animation its own row, phase 4, called "tile animation".

What a group is

Each tileset keeps a list of its animated groups, in the order they were written down. tile_anim_frame and tile_anim_pause need to know which group you mean, and the group's number is its place in that list, counting from 0. Nothing else. The first group on the list is group 0, the second is group 1.

Say a tileset lists three groups: water, then a torch, then a conveyor belt.

Written downGroup numberWhat it animates
1st0water
2nd1torch
3rd2conveyor belt

tile_anim_frame(that_tileset, 1, 0) jumps the torch to its first picture, not the water. The number does not say "torch" anywhere. To know which is which, you have to remember the order you wrote them in. A group number that does not exist does nothing and gives no error.

Setting one up

You set it up once, on the tileset, in the Paint tab. Open the tileset there and look for the Animated tiles section in the left panel.

  1. Press + Animate a tile, then click the tile on the picture that should move (water, say).
  2. The pictures of that tile appear in the drawer under the canvas. Press Add a picture and click the tiles that hold the other pictures, one after another. Draw those pictures on empty squares of the tileset first: a picture is just another tile, and you never paint it in a room. Add a copy makes a new picture that starts as a copy of the picked one.
  3. Set the speed: how many frames each picture stays. A frame is one picture the GBA draws, 60 a second, so 12 means five steps a second. 0 means it never moves by itself, so only tile_anim_frame moves it.
  4. Play on the picture shows the tile moving right there, the way the game will.
  5. Save the picture (Ctrl+S).

Now paint in the Room Editor as you always do. Every cell that uses the tile moves, in every room that uses the tileset. The Room Editor plays it in its own view, too (the box Play animated tiles, at the bottom of Tilesets, in the Tile Palette section, switches that off). Nothing in the rooms changes: the cells still just say "tile 1".

The tiles are numbered as the Paint tab shows them. On a tileset you paint rooms from, the number is the square's place counting from 0 along the first row and then the next rows, so the first square is tile 0 and nothing is merged or skipped. A whole-picture background (one picture a room shows as it is) is numbered by the console's own rules, with a blank tile first: its first painted tile is tile 1.

Ripples: copies that start later

One tile shows one picture at a time, so every cell painted with it moves in step. To make water or grass roll instead, you need the same animation starting at another picture. In the Animated tiles section, pick the animated tile and press Add the copy after setting how many pictures later it should start (1 is one picture behind). Gbabel puts a new tile on an empty square of the tileset, filled with the picture it starts on, and makes a group for it that plays the same pictures from there. A copy costs one tile and no new art.

In the Room Editor, at the bottom of Tilesets, in the Tile Palette section, tick Mix in offset copies of an animated tile and paint with the first tile: each cell you paint (or fill) gets the tile or one of its copies, chosen by the cell's place, so the same cell always gets the same one. A tile counts as a copy when its group has the same pictures, turned round, at the same speed. A shipped game can paint the copies by hand instead, with the same effect.

What it costs in tiles

Every picture of an animated tile is a square of the tileset, and a tileset you paint rooms from keeps all its squares in the console's tile memory. So each picture counts in the 1,024 tiles of Budgets. A copy that starts later adds one tile.

Code control

tile_anim_pause and tile_anim_frame work the same way on a tileset you paint rooms from. The group's number is its place in that tileset's list of animated tiles, counting from 0 (the Paint tab lists them in that order and says the number).

Steering it from code

A group runs by itself. Two functions let your code take over for a moment.

Here a lever snaps the torch, group 1, back to its first picture the moment it is pulled:

// Step event of a lever
if (btn_pressed(BTN_A)) {
    tile_anim_frame(ts_doc_dungeon, 1, 0);   // group 1 is the torch: show picture 0
}

tile_anim_frame does not pause anything. The group's own timer starts again from zero, so the torch carries on from there on its normal beat. To hold a picture still, pause the tileset as well.

Here the water holds still for as long as A is held down. Calling tile_anim_pause every frame is fine, because asking for the state it is already in does nothing:

// Step event of an object that is always in the room
tile_anim_pause(ts_doc_dungeon, btn_held(BTN_A));

Tile animation does stop when the game is paused (game_pause), because Project Settings has a box, Stop animations on pause, and it is ticked in every project. The water holds the picture it was showing, and carries on from it when the game does. If you unticked that box, the water keeps flowing behind a pause menu, and you can freeze it yourself by calling tile_anim_pause.

A group keeps counting even when its tileset is not in the room you are in. Leave a room and come back, and the group is wherever the count has reached. The picture itself is only copied for the tileset of the room that is loaded right now.

What it cannot do

Animated backgrounds

An animated background is a whole picture that moves - a title screen with swaying hair, a flickering fire and a tide, or any calm scene. Make one in the Paint tab with Add animated background: add its frames (pictures of the same size), give each frame the time it stays up, and press Preview. Preview says how many 8 by 8 squares change, how many of the 1,024 shared tiles the picture takes, how much cartridge space the other frames need and how many tiles a step copies, and plays the result right there. Nothing changes until you press Accept.

Only the squares that change from frame to frame are repainted; the others are ordinary tiles. The other frames of the changing squares stay on the cartridge and are copied in as they play, so they do not use the 1,024 shared tiles. Because the console can only copy so many tiles in a frame, an animated background is best for title screens and calm scenes, not busy rooms where most of the picture moves.

Put it in a room like any background (Backgrounds in the Room Editor), where you also set how fast it plays and whether it starts playing when the room opens. From code, background_animation_play, background_animation_pause, background_animation_speed and background_animation_frame control it; a cutscene has Play animated background and Pause animated background steps.

See also: tile_anim_frametile_anim_pausetile_set_indexOne picture, several palettestile_set_classRoomsRooms, tiles and tile animationBudgetsThe frame profiler